/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick.task;

import com.appspot.ittrick.drive.trick.api.DriveAPIService;
import com.appspot.ittrick.drive.trick.controller.MainController;
import com.google.api.services.drive.model.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

/**
 *
 * @author takahara
 */
public class FileDownloadTask extends Service<Object> {

    private java.io.File loalFile;
    private File downloadFile;

    public void run(java.io.File loalFile, File downloadFile) {
        this.loalFile = loalFile;
        this.downloadFile = downloadFile;
        this.restart();
    }

    @Override
    protected Task<Object> createTask() {
        return new Task<Object>() {
            @Override
            protected Object call() throws Exception {
                
                if (downloadFile.getMimeType().equals("application/vnd.google-apps.folder")) {
                    loalFile.mkdir();
                    createDri(loalFile, downloadFile);
                }else {
                    createFile(loalFile, downloadFile);
                }

                return null;
            }

            private void createDri(java.io.File dir, File downloadFile) throws IOException {
                
                updateMessage(dir.getName());
                
                DriveAPIService driveService = MainController.getInstance().getDriveService();

                List<File> childrenFileList = driveService.getChildrenFilesList(downloadFile.getId());

                for (File childrenFile : childrenFileList) {
                    
                    String filePath = dir.getPath() + "/" + childrenFile.getTitle();
                    java.io.File outPutFile = new java.io.File(filePath);
                    
                    if (childrenFile.getMimeType().equals("application/vnd.google-apps.folder")) {
                        // 出力
                        outPutFile.mkdir();
                        // 再帰
                        createDri(outPutFile, childrenFile);
                    } else {
                        createFile(outPutFile, childrenFile);
                    }
                }
            }

            private void createFile(java.io.File localFile, File downloadFile) throws FileNotFoundException, IOException {
                
                updateMessage(downloadFile.getTitle());
                
                DriveAPIService driveService = MainController.getInstance().getDriveService();
                InputStream inputStream = driveService.downloadFile(downloadFile);

                // InputStream からファイルを出力
                byte[] buffer = new byte[1024];
                int length;
                FileOutputStream fos = null;

                try {
                    fos = new FileOutputStream(localFile);

                    while ((length = inputStream.read(buffer)) >= 0) {
                        fos.write(buffer, 0, length);
                    }

                    fos.close();
                    fos = null;
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                        }
                    }
                }
            }
        };
    }
}
